/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.generic_return_type;

import moved.org.objectweb.asm.Label;
import moved.org.objectweb.asm.MethodVisitor;
import picounit.features.classloader.NullVisitor;

class CheckCastMethodVisitor extends NullVisitor implements MethodVisitor {
	private final CheckCastRegistry checkCastRegistry;

	private String className;
	private String methodName;

	private int lineNumber;

//	private String previousMethodOwner;
//	private String previousMethodName;
//	private String previousMethodDesc;
//
	public CheckCastMethodVisitor(CheckCastRegistry checkCastRegistry) {
		this.checkCastRegistry = checkCastRegistry;
	}

	public MethodVisitor visit(String className, String methodName) {
		this.className = className;
		this.methodName = methodName;

		return this;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// MethodVisitor
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public void visitLineNumber(int lineNumber, Label start) {
		this.lineNumber = lineNumber;
	}

	@Override
	public void visitMethodInsn(int opcode, String owner, String name, String desc) {
		if (opcode != INVOKEVIRTUAL) {
			return;
		}
//
//		this.previousMethodOwner = owner;
//		this.previousMethodName = name;
//		this.previousMethodDesc = desc;
	}

	@Override
	public void visitTypeInsn(int opcode, String desc) {
		if (opcode == CHECKCAST && previousMethodReturnImplementedByDynamicProxy()) {
			checkCastRegistry.addCheckCast(className, methodName, lineNumber, desc.replace('/', '.'));
		}
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation
	///////////////////////////////////////////////////////////////////////////////////////////////

	// TODO: Return true when method is MockFactory.mock
	private boolean previousMethodReturnImplementedByDynamicProxy() {
		return true;
	}
}
